import JSEncrypt from 'jsencrypt';
import CryptoJS from 'crypto-js';
import { isString } from 'is-what';

const iv = "1234567890000000";
const secretKey = "zhonghankeji0123";

export function aesEncrypt(content) {
    if (content === '') { return '' }
    let key = CryptoJS.enc.Utf8.parse(secretKey);
    let srcs = CryptoJS.enc.Utf8.parse(content);
    let encrypted = CryptoJS.AES.encrypt(srcs, key, { mode:CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
    return encrypted.toString();
}
export function aesDecrypt(encrypted) {
    if (!isString(encrypted) || encrypted === '') { return '' }
    let key = CryptoJS.enc.Utf8.parse(secretKey);
    let decrypt = CryptoJS.AES.decrypt(encrypted, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
    return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}

const pubkey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwxEw2UOtYgOcX9Jqa8uoivxPtOz9chEn5DNGjZGJXRJuXPZcf93pTGGDtakN2xNfnuNPdeOeLDmj/eN8Q9bJke1F2cGIQCZnLHQzrksxDglQP2qmxXLauTV3CojM9VFtYQChAUuVIr0BCSl9LyKuPL3kwi35GhrSKG3ztIlehlQIDAQAB"

export function rsaEncrypt(content) {
    const encryptor = new JSEncrypt(); // 新建JSEncrypt对象
    encryptor.setPublicKey(pubkey); // 设置公钥
    return encryptor.encrypt(content) // 对需要加密的数据进行加密
}

export function rsaDecrypt(content)  {
    const decrypt = new JSEncrypt();
    decrypt.setPrivateKey(pubkey);
    return decrypt.decrypt(content) // 解密
}

export function isBase64Encode(content) {
    return /^[A-Za-z0-9+/]*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$/.test(content)
}